package de.hdu.pms.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Test;
import de.hdu.pms.model.Basis;

public class BasisDaoTest extends AbstractDataAccessTest {

	public static Logger Log = Logger.getLogger(BasisDaoTest.class);

	public static void setLog(Logger log) {
		Log = log;

	}

	private BasisDao basisDao = null;
	private String tableName = "basis";

	public void setBasisDao(BasisDao basisDao) {
		this.basisDao = basisDao;
	}

	@Test
	public void testFindById() {
		Log.info("UnitTest - Basis - FindById");
		Basis basis = new Basis();
		basis.setBasis_bez("test");
		basisDao.save(basis);
		List<Basis> alleBasen = basisDao.findAll();
		for (Basis b : alleBasen) {
			int id = b.getId();
			Basis found = basisDao.findById(id);
			assertEquals(b.getBasis_bez(), found.getBasis_bez());
		}

	}

	@Test
	public void testFindAll() {
		Log.info("UnitTest - Basis - FindAll");
		// Wurde bereits in testFindById() getestet
		testFindById();
	}

	@SuppressWarnings("deprecation")
	@Test
	public void testSave() {
		Log.info("UnitTest - Basis - Save");
		String[] tables = { tableName };
		deleteFromTables(tables);
		Basis basis = new Basis();
		basis.setBasis_bez("save");
		basisDao.save(basis);
		int rows = countRowsInTable(tableName);
		assertEquals("Es darf nur eine Zeile vorhanden sein", 1, rows);

	}

	@SuppressWarnings("deprecation")
	@Test
	public void testDelete() {
		Log.info("UnitTest - Basis - Delete");
		String[] tables = { tableName };
		deleteFromTables(tables);
		Basis basis = new Basis();
		basis.setBasis_bez("delete");
		basisDao.save(basis);
		try {
			basisDao.delete(basis);
		} catch (Exception e) {
			// TODO: handle exception
			e.getMessage();
		}
		List<Basis> alle = basisDao.findAll();
		assertEquals(0, alle.size());
		deleteFromTables(tables);
	}

}
